//
//  PlistHandler.m
//  OCBase
//
//  Created by 侯伟 on 2019/5/7.
//  Copyright © 2019 侯伟. All rights reserved.
//

#import "PlistHandler.h"

@implementation PlistHandler

static NSString* _curUser;

+ (void)setCurUser:(NSString *)curUser{
    _curUser = curUser;
}

+ (NSString*)curUser{
    return _curUser;
}

/**
 文档文件
 
 @param fileName -文件名称
 @return 文件路径
 */
+(NSString*)documentationFile:(NSString*)fileName{
    
    NSString* dir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
    
    if(PlistHandler.curUser){
        dir = [dir stringByAppendingPathComponent:PlistHandler.curUser];
        BOOL isDir;
        if(![[NSFileManager defaultManager] fileExistsAtPath:dir isDirectory:&isDir]){
            [[NSFileManager defaultManager] createDirectoryAtPath:dir withIntermediateDirectories:YES attributes:NULL error:NULL];
        }
    }
    NSString* file = [dir stringByAppendingPathComponent:fileName];
    
    if(![[NSFileManager defaultManager] fileExistsAtPath:file]){
       [[NSFileManager defaultManager] createFileAtPath:file contents:NULL attributes:NULL];
    }

    return file;
}


/**
 删除文档文件

 @param fileName -文件名称
 @return 文件路径
 */
+(BOOL)removeDocumentationFile:(NSString*)fileName{
    NSString* file = [self documentationFile:fileName];
    
    if([[NSFileManager defaultManager] fileExistsAtPath:file]){
        return [[NSFileManager defaultManager] removeItemAtPath:file error:NULL];
    }
    return YES;
}

/**
 缓存文件
 
 @param fileName -文件名称
 @return 文件路径
 */
+(NSString*)cacheFile:(NSString*)fileName{
    
    NSString* dir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
    
    if(PlistHandler.curUser){
        dir = [dir stringByAppendingPathComponent:PlistHandler.curUser];
        BOOL isDir;
        if(![[NSFileManager defaultManager] fileExistsAtPath:dir isDirectory:&isDir]){
            [[NSFileManager defaultManager] createDirectoryAtPath:dir withIntermediateDirectories:YES attributes:NULL error:NULL];
        }
    }
    NSString* file = [dir stringByAppendingPathComponent:fileName];
    
    if(![[NSFileManager defaultManager] fileExistsAtPath:file]){
        [[NSFileManager defaultManager] createFileAtPath:file contents:NULL attributes:NULL];
    }
    return file;
}

/**
 删除缓存文件

 @param fileName -文件名称
 @return 文件路径
 */
+(BOOL)removeCacheFile:(NSString*)fileName{
    NSString* file = [self cacheFile:fileName];
    
    if([[NSFileManager defaultManager] fileExistsAtPath:file]){
        return [[NSFileManager defaultManager] removeItemAtPath:file error:NULL];
    }
    
    return YES;
}

/**
 从文档文件获取字典
 
 @param fileName 文件名称
 @return 字典
 */
+(NSDictionary*)dictionaryOfdocumentationFile:(NSString*)fileName{
    
    return [NSDictionary dictionaryWithContentsOfFile:[PlistHandler documentationFile:fileName]];
}

/**
 从缓存文件获取字典
 
 @param fileName 文件名称
 @return 字典
 */
+(NSDictionary*)dictionaryCacheFile:(NSString*)fileName{
    return [NSDictionary dictionaryWithContentsOfFile:[PlistHandler cacheFile:fileName]];
}

/**
 从文档文件获取数组
 
 @param fileName 文件名称
 @return 数组
 */
+(NSArray*)arrayOfdocumentationFile:(NSString*)fileName{
    return [NSArray arrayWithContentsOfFile:[PlistHandler documentationFile:fileName]];
}

/**
 从缓存文件获取数组
 
 @param fileName 文件名称
 @return 数组
 */
+(NSArray*)arrayCacheFile:(NSString*)fileName{
    return [NSArray arrayWithContentsOfFile:[PlistHandler cacheFile:fileName]];
}

 /*从文档文件获取数据

 @param fileName 文件名称
 @return 字典
 */
+(NSData*)dataOfdocumentationFile:(NSString*)fileName{
    return [NSData dataWithContentsOfFile:[PlistHandler documentationFile:fileName]];
}

/**
 从缓存文件获取字数据
 
 @param fileName 文件名称
 @return 字典
 */
+(NSData*)dataCacheFile:(NSString*)fileName{
    return [NSData dataWithContentsOfFile:[PlistHandler cacheFile:fileName]];
}



@end
